﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ShuDou
{
    public class Cellsd
    {
        public Cellsd()
        {
            Clear();
            _crg = null;
        }
        public void Clear()
        {
            V = -1;
            IsOrigin = false;
            CanList = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            ExistList = new List<int>();
        }
        public void InitCanList()
        {
            CanList.Clear();
            ExistList.Clear();
            for (int i = 1; i < 10; i++)
                if (C.ExistList.Contains(i) || R.ExistList.Contains(i) || G.ExistList.Contains(i))
                    ExistList.Add(i);
                else
                    CanList.Add(i);
        }
        public override string ToString()
        {
            if (V == -1)
            {
                if (ShowCanList)
                    if (CanList != null && CanList.Count > 0)
                    {
                        string str = "";
                        foreach (int i in CanList)
                            str += i;
                        return str;
                    }

                return "";
            }
            return V.ToString();
        }
        public List<Cellsd> CanListCell
        {
            get
            {
                if (CRG == null)
                    return new List<Cellsd>();
                return CRG.Where(r => r.V < 0).ToList();
            }
        }
        public int V { get; set; }
        public List<int> CanList { get; set; }
        public List<int> ExistList { get; set; }
        public Boolean IsOrigin { get; set; }

        private List<Cellsd> _crg;
        public List<Cellsd> CRG
        {
            get
            {
                if (C == null || R == null || G == null)
                    return null;
                if (_crg == null)
                {
                    List<Cellsd> t_crg = new List<Cellsd>();
                    t_crg.AddRange(R.list);
                    t_crg.AddRange(C.list);
                    foreach (Cellsd cc in G.list)
                        if (!t_crg.Contains(cc))
                        {
                            t_crg.Add(cc);
                        }
                    //_crg.AddRange(G.list);
                    //_crg =  _crg.Distinct((r1,r2) => r1 == r2  );
                    t_crg.Remove(this);
                    if (t_crg.Count > 20)
                        _crg = t_crg;
                    else
                        ;
                }
                return _crg;
            }
        }
        public CellR R { get; set; }
        public CellC C { get; set; }
        public CellG G { get; set; }
        public static bool ShowCanList;

        public  void SetValueByCanList()
        {
            V = CanList[0];
            CanList.Clear();
            foreach (Cellsd cc in  CanListCell)
                if (cc.CanList.Contains(V))
                    cc.CanList.Remove(V);
            List<CellList> LCRG = new List<CellList>() { C, R, G };
            foreach (CellList cl in LCRG)
                cl.AddExistValue(V);
        }
        public List<CellList> LCRG
        {
            get { return new List<CellList>() { C, R, G }; }
        }
        //////

    }
    public class CellList
    {
        public CellList()
        {
            CanList = new List<int>();
            ExistList = new List<int>();
            _list = new List<Cellsd>();
            PJ = new List<int>();
        }
        public Boolean IsError { get; set; }
        public List<int> CanList { get; set; }
        public List<int> ExistList { get; set; }
        public void InitExistCanList()
        {
            IsError = false;
            CanList.Clear();
            ExistList.Clear();
            foreach (Cellsd C in list)
            {
                if (C.V > 0)
                {
                    if (ExistList.Contains(C.V))
                        IsError = true;
                    else
                        ExistList.Add(C.V);
                }
                else
                {
                    if (C.CanList.Count == 0)
                        IsError = true;
                }
            }
            for (int i = 1; i < 10; i++)
                if (!ExistList.Contains(i))
                    CanList.Add(i);
        }

        public virtual List<Cellsd> list { get { return _list; } }
        public override string ToString()
        {
            return _Index.ToString();
        }
        protected List<Cellsd> _list;
        protected int _Index;

        public static Cellsd[,] _Data;

        public  void AddExistValue(int V)
        {
            if (!ExistList.Contains(V))
            {
                ExistList.Add(V);
                CanList.Remove(V);
            }
            else
            {
                throw new Exception("逻辑错误");
            }
        }
        /////
        public void ClearPJ()
        {
            PJ.Clear();
        }
        public void AddPJ(int vi)
        {
            PJ.Add(vi);
        }
        public void RemovePJ(int vi)
        {
            PJ.Remove(vi);
        }
        public List<int> PJ;
    }
    public class CellR : CellList
    {
        public CellR(int rowindex)
        {
            _list = null;
           _Index = rowindex;
        }
        public int RowIndex { get { return _Index; } }
        public override List<Cellsd> list
        {
            get
            {
                if (_list == null)
                {
                    if (_Data != null && RowIndex != -1)
                    {
                        _list = new List<Cellsd>();
                        for (int ColIndex = 0; ColIndex < 9; ColIndex++)
                        {
                            _list.Add(_Data[RowIndex, ColIndex]);
                        }
                    }
                    else
                    {
                        System.Windows.Forms.MessageBox.Show("RERROR");
                    }
                }
                return _list;
            }
        }
    }
    public class CellC : CellList
    {
        public CellC(int colindex)
        {
            _Index = colindex;
            _list = null;
        }
        public int ColIndex { get { return _Index; } }
        public override List<Cellsd> list
        {
            get
            {
                if (_list == null)
                {
                    if (_Data != null && ColIndex != -1)
                    {
                        _list = new List<Cellsd>();
                        for (int RowIndex = 0; RowIndex < 9; RowIndex++)
                        {
                            _list.Add(_Data[RowIndex, ColIndex]);
                        }
                    }
                    else
                    {
                        System.Windows.Forms.MessageBox.Show("CERROR");
                    }
                }
                return _list;
            }
        }
    }
    public class CellG : CellList
    {
        public CellG(int gongindex)
        {
            _Index = gongindex;           
        }
        public int GongIndex { get { return _Index; } }        
        public void Addlist(Cellsd c)
        {
            _list.Add(c);
        }
    }
}
